package cn.bubi.sdk.sample;

import cn.bubi.access.adaptation.blockchain.bc.OperationTypeV3;
import cn.bubi.access.adaptation.blockchain.bc.response.Account;
import cn.bubi.access.adaptation.blockchain.bc.response.ledger.Ledger;
import cn.bubi.access.utils.blockchain.BlockchainKeyPair;
import cn.bubi.access.utils.blockchain.SecureKeyGenerator;
import cn.bubi.sdk.core.config.SDKConfig;
import cn.bubi.sdk.core.config.SDKProperties;
import cn.bubi.sdk.core.exception.SdkException;
import cn.bubi.sdk.core.interfaces.RpcServiceWithIdx;
import cn.bubi.sdk.core.operation.impl.CreateAccountOperation;
import cn.bubi.sdk.core.spi.BcOperationService;
import cn.bubi.sdk.core.spi.BcQueryService;
import cn.bubi.sdk.core.transaction.Transaction;
import cn.bubi.sdk.core.transaction.model.TransactionCommittedResult;
import cn.bubi.sdk.core.utils.GsonUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author xiezhengchao@bubi.cn
 * @since 17/10/25 下午3:34.
 */
public class Demo{

    //    创始者
    private static String address = "a001b2a709247a72c8324d04adc0141b622e3a0c5cc89b";
    private static String publicKey = "b0017d9c341ba14d0bb18518721c332c4ec1938b87661af8cab4c0a172075e7a04b36b";
    private static String privateKey = "c0014da554d65813edf19946f95d65d6b710f8288df87435a82b26b4341fcc1fb95cc5";

    public static void main(String[] args) throws SdkException{
        Float f  = 40.1F;
        Float t = 59.9F;
        if(f + t == 100.F) {
            System.out.println("True");
        }

//        String eventUtis = "ws://101.200.57.7:8080,ws://101.200.57.7:8083,ws://101.200.57.7:8085";
//        String ips = "101.200.57.7:8082,101.200.57.7:8084,101.200.57.7:8086";
//
//        SDKConfig config = new SDKConfig();
//        SDKProperties sdkProperties = new SDKProperties();
//        sdkProperties.setEventUtis(eventUtis);
//        sdkProperties.setIps(ips);
//        sdkProperties.setAccountPoolEnable(false);
//        sdkProperties.setAddress(address);
//        sdkProperties.setPublicKey(publicKey);
//        sdkProperties.setPrivateKey(privateKey);
//        sdkProperties.setSize(12);
//        sdkProperties.setMark("test-demo-config");
//        sdkProperties.setRedisSeqManagerEnable(false);
//        sdkProperties.setHost("192.168.10.73");
//        sdkProperties.setPort(10379);
//        sdkProperties.setPassword("bubi888");
//        config.configSdk(sdkProperties);
//
//
//        RpcServiceWithIdx rpcService = config.getRpcService();
//        rpcService.setIdx(1);
//
//        for(int i = 0 ; i < 100 ;i++) {
//            Ledger ledger = rpcService.getLedger();
//            System.out.println("idx: " + i + "val:" + GsonUtil.toJson(ledger));
//        }
//        rpcService.disableIdx();


        // 进行查询
//        BcQueryService queryService = config.getQueryService();
//        Account account = queryService.getAccount(address);
//        System.out.println(GsonUtil.toJson(account));
//
//        // 简单操作
//        createAccountOperation(config.getOperationService());

//        BcQueryService queryService2 = config.getQueryService();
//        config.getRpcService().setIdx(2);
//        queryService.getAccount(address);
//        config.getRpcService().disableIdx();
//        queryService.getAccount(address);
//        config.getRpcService().setIdx(1);
//        queryService.getAccount(address);
//        config.getRpcService().setIdx(0);
//        queryService2.getAccount(address);
    }

    /**
     * 创建账户操作
     */
    private static void createAccountOperation(BcOperationService operationService){
        try {
            Transaction transaction = operationService.newTransaction(address);

            BlockchainKeyPair keyPair = SecureKeyGenerator.generateBubiKeyPair();
            System.out.println(GsonUtil.toJson(keyPair));

            CreateAccountOperation createAccountOperation = new CreateAccountOperation.Builder()
                    .buildDestAddress(keyPair.getBubiAddress())
                    .buildScript("function main(input) { /*do what ever you want*/ }")
                    .buildAddMetadata("自定义key1", "自定义value1").buildAddMetadata("自定义key2", "自定义value2")
                    // 权限部分
                    .buildPriMasterWeight(15)
                    .buildPriTxThreshold(15)
                    .buildAddPriTypeThreshold(OperationTypeV3.CREATE_ACCOUNT, 8)
                    .buildAddPriTypeThreshold(OperationTypeV3.SET_METADATA, 6)
                    .buildAddPriTypeThreshold(OperationTypeV3.ISSUE_ASSET, 4)
                    .buildAddPriSigner(SecureKeyGenerator.generateBubiKeyPair().getBubiAddress(), 10)
                    .build();

            TransactionCommittedResult result = transaction.buildAddOperation(createAccountOperation)
                    .buildTxMetadata("交易metadata")
                    .buildAddSigner(publicKey, privateKey)
                    .commit();

            System.out.println("\n------------------------------------------------");
            System.out.println(GsonUtil.toJson(result));
        } catch (SdkException e) {
            e.printStackTrace();
        }
    }

}
